package com.atguigu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.entity.EduCourse;
import com.atguigu.entity.EduTeacher;
import com.atguigu.entity.vo.CourseInfoVo;
import com.atguigu.entity.vo.CoursePublishVo;
import com.atguigu.entity.vo.CourseQuery;
import com.atguigu.service.EduCourseService;
import com.atguigu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wxc
 * @since 2021-07-04
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduTeacherService teacherService;

    //课程列表
    @GetMapping("/getAllCourseList")
    public R getAllCourseList(){
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list", list);
    }

    //课程列表条件分页查询
    @PostMapping("/pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable Long current,
                                 @PathVariable Long limit,
                                 @RequestBody CourseQuery courseQuery){


        //查询教师
        String teacherName = courseQuery.getTeacherName();
        QueryWrapper<EduTeacher> teacherWrapper = new QueryWrapper<>();

        if(teacherName != null){
            teacherWrapper.like("name", teacherName);
        }

        List<EduTeacher> teacherList = teacherService.list(teacherWrapper);
        System.out.println(teacherList);

        List<String> teacherIdList = new ArrayList<>();

        for (EduTeacher teacher : teacherList) {
            teacherIdList.add(teacher.getId());
        }


        Page<EduCourse> page = new Page<>(current, limit);

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        String title = courseQuery.getTitle();

        String status = courseQuery.getStatus();

        if(title != null){
            wrapper.like("title", title);
        }
        if(status != null){
            wrapper.eq("status", status);
        }
        if(teacherIdList.size() > 0){
            wrapper.in("teacher_id", teacherIdList);
        }

        courseService.page(page, wrapper);

        //查询的结果
        List<EduCourse> records = page.getRecords();
        Long total = page.getTotal();

        

        return R.ok().data("total", total).data("list", records);
    }

    //添加课程基本信息的方法
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后课程id，为了后面添加大纲使用
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    //根据课程id查询课程的基本信息
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    //修改课程基本信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }

    //课程最终发布
    //修改课程状态
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        //设置课程发布的状态
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //删除课程
    @DeleteMapping("/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }

}

